package com.liang.controller;
/*
 *@Author: ljy
 *@Date:2021/11/3 21:52
 *
 */

import com.liang.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 通过redis实现对实体的增删改查
 */
@RestController
public class StudentHandler {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 将JSON字符串序列化为student，存入redis中
     *
     * @param student
     */
    @PostMapping("/set")
    //将客户端传过来的JSON 字符串 转换为student对象 通过@RequestBody注解
    public void set(@RequestBody Student student) {
        redisTemplate.opsForValue().set("student", student);
    }

    /**
     * 将数据从redis服务器中取出来，存的时候是序列化，取得时候是反序列化
     *
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public Student get(@PathVariable("key") String key) {
        return (Student) redisTemplate.opsForValue().get(key);
    }

    /**
     * @param key
     * @return 布尔值
     * 返回false 就代表删掉了
     */
    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("key") String key) {
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);

    }

    /**
     * 字符串类型
     */
    @GetMapping("/string")
    public String stringTest() {
        redisTemplate.opsForValue().set("str", "HelloWorld");
        String str = (String) redisTemplate.opsForValue().get("str");
        return str;
    }

    /**
     * 列表 就是指list
     * 看成栈
     */
    @GetMapping("/list")
    public List<String> listTset() {
        ListOperations<String, String> listOperations = redisTemplate.opsForList();//ListOperations<K,v>
        //存redis
        listOperations.leftPush("list", "Hello");
        listOperations.leftPush("list", "world");
        listOperations.leftPush("list", "java");
        //取redis
        /**
         * 1代表从world 拿数据
         * 2代表从world 取数据
         */
        List<String> list = listOperations.range("list", 1, 2);
        return list;
        /**
         * 这不左边进右边取嘛
         * [
         *     "world",
         *     "Hello"
         * ]
         */
    }

    /**
     * 集合
     */
    @GetMapping("/set")
    public Set<String> setTest() {
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        setOperations.add("set", "Hello");
        setOperations.add("set", "Hello");
        setOperations.add("set", "World");
        setOperations.add("set", "World");
        setOperations.add("set", "java");
        setOperations.add("set", "java");
        //返回成员
        Set<String> set = setOperations.members("set");
        return set;
        /**
         *   "java",
         *   "Hello",
         *   "World"
         */
    }

    @GetMapping("/zset")
    public Set<String> zsetTest() {
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        //因为zset是有序的所以必须参数加个序号
        zSetOperations.add("zset", "Hello", 1);
        zSetOperations.add("zset", "World", 2);
        zSetOperations.add("zset", "Java", 3);
        //取值
        Set<String> zset = zSetOperations.range("zset", 0, 2);
        return zset;
        /**
         * [
         *     "Hello",
         *     "World",
         *     "Java"
         * ]
         */
    }

    @GetMapping("/hash")
    public void hashTest() {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("rediskey", "hashkey", "hello");
        System.out.println(hashOperations.get("rediskey", "hashkey"));

    }
}
